// components/popup/recharge/index.js
import {
  PayHttp
} from '../../../api/pay.js'
import Utils from '../../../utils/utils'

const payModel = new PayHttp();

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    show: Boolean
  },

  /**
   * 组件的初始数据
   */
  data: {
    payment: '3',
    paymentVal: '1',
    paymentMoney: 50,
    checkedIcon: {
      active: '/images/icon/icon_check_active.png',
      normal: '/images/icon/icon_check_inactive.png',
      normalEmpty: '/images/icon/icon_check_inactive_empty.png'
    }
  },

  /**
   * 组件的方法列表
   */
  methods: {
    // 关闭
    onClose () {
      this.triggerEvent('close')
    },
    // 选择充值金额
    onSelectAmount (event) {
      const { type, money } = event.currentTarget.dataset
      this.setData({ paymentVal: type, paymentMoney: money });
    },
    // 选择支付方式
    onSelectPayment (event) {
      const { name } = event.currentTarget.dataset
      this.setData({ payment: name });
    },
    // 确认充值
    onConfirmRecharge () {
      payModel.memberPay({
        "money_pay": this.data.paymentMoney * 100, //要充值的金额，单位分
        "soft_id": 2, // 软件id APP填写0 巷友小程序填写2
        "pay_type": 3, //支付类型  2:支付宝 3：微信 4:会员卡
      }).then((res) => {
        console.log('res', res)
        wx.requestPayment({
          timeStamp: res.mini.timeStamp,
          nonceStr: res.mini.nonceStr,
          package: res.mini.package,
          signType: res.mini.signType,
          paySign: res.mini.paySign,
          success (res) {
            Utils.msg('充值成功')
            this.triggerEvent('close')
          },
          fail (res) {
            Utils.msg('充值失败')
          }
        })
      })
    }
  }
})
